/**
 * A component for rendering information about a mazlow dataset profile
 */
Ext.define('okcapital.widgets.MazlowProfilerPanel', {
    extend: 'Ext.panel.Panel',
    
    alias : 'widget.mazlowprofilerpanel',

    /** Instance of Ext.data.Store of okcapital.model.Dataset instances*/
    datasetStore : null,
    
    /** an array of array of DatasetProfile models. Indexed at the top level by their mazlow rank*/
    profilesByRank : null,

    constructor : function(cfg) {
        this.datasetStore = cfg.datasetStore;
    
        this.addEvents({
        	'filterchange' : true
        });
        
        this.initialiseProfilesByRank();
        this.datasetStore.on('load', this.initialiseProfilesByRank, this);
        
        
        var spriteConfigs = [];
        for (var i = 0; i < okcapital.widgets.MazlowUtil.mazlowLevels; i++) {
        	spriteConfigs = spriteConfigs.concat(okcapital.widgets.MazlowUtil.generateMazlowSprites( 
        			i, 
        			undefined, 
        			Ext.bind(this.onMazlowClick, this), 
        			Ext.bind(this.onMazlowMouseover, this)));
        }
        
        Ext.apply(cfg, {
        	layout : 'fit',
        	items : [{
        		xtype : 'draw',
        		items : spriteConfigs
        	}]
        });
        
        this.callParent(arguments);
    },
    
    initialiseProfilesByRank : function() {
    	this.profilesByRank = [[], [], [], [], []];
        for (var i = 0; i < this.datasetStore.getCount(); i++) {
    		var ds = this.datasetStore.getAt(i);
    		
    		this.profilesByRank[ds.get('mazlowRank') - 1].push(Ext.create('okcapital.model.DatasetProfile', {
    			datasetId : ds.get('datasetId'),
    			weighting : 3 //everything gets default weighting to begin with
    		}));
    	}
    },
    
    onMazlowClick : function(sprite) {
    	//Generate GUI elements
    	var items = [];
    	var profiles = this.profilesByRank[sprite._mazlowLevel];
    	var me = this;
    	for (var i = 0; i < profiles.length; i++) {
    		var profile = profiles[i];
    		var ds = this.datasetStore.getById(profile.get('datasetId'));
    		
    		if (ds) {
	    		items.push({
	    			xtype : 'sliderfield',
	    			fieldLabel : ds.get('name'),
	    			name : ds.get('datasetId'),
	    			maxValue : 5,
	    			minValue : 1,
	    			value : profile.get('weighting'),
	    			_profile : profile,
	    			increment : 1,
	    			width : 300,
	    			listeners : {
	    				change : function(slider) {
	    					slider._profile.set('weighting', slider.getValue());
	    					
	    					me.fireEvent('filterchange', me);
	    				}
	    			} 
	    		});
    		}
    	}
    	
    	var popup = Ext.create('Ext.Window', {
    		title : 'Choose your weightings for ' + okcapital.widgets.MazlowUtil.mazlowNames[sprite._mazlowLevel],
    		width : 400,
    		height : 300,
    		layout : 'fit',
    		items : [{
    			xtype : 'fieldset',
    			title : '1 is no relevance, 5 is high relevance',
    			items : items
    		}]
    	});
    	
    	popup.show();
    },
    
    onMazlowMouseover : function() {
    	
    },
    
    /**
     * Gets all DatasetProfile models as configured by this panel
     */
    getDatasetProfiles : function() {
    	var result = [];
    	for (var level = 0; level < okcapital.widgets.MazlowUtil.mazlowLevels; level++) {
    		var profiles = this.profilesByRank[level];
	    	for (var i = 0; i < profiles.length; i++) {
	    		result.push(profiles[i]);
	    	}
    	}
    	
    	return result;
    }
   


});